package cn.pconline.autoclub.task;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.pconline.autoclub.task.util.Pinyin4jUtil;


/**
 * 初始化车友会品牌数据任务
 * @author zhoutianhuai
 *
 */
public class InitClubBrandTask {
  private static Logger log = LoggerFactory.getLogger(InitClubBrandTask.class);
  /**活动所有车友会品牌ID和名称(排重)sql**/
  private String getAllClubBrandSql = "SELECT  distinct brand_id, brand_name from ac_club";
  private String updateClubBrandSql = " insert into ac_club_brand (brand_id, brand_name, initial, create_at) values (?,?,?,now())"
		                            +"  ON DUPLICATE KEY UPDATE update_at = now()";
  public static void setLog(Logger log) {
	  InitClubBrandTask.log = log;
  }
  
  public static void main(String[] args) throws SQLException {
	  //初始化Log
      AppUtils.initLog(InitClubBrandTask.class);
      InitClubBrandTask t = new InitClubBrandTask();
      AppUtils.log.info("run initClubBrand start...");
      t.initClubBrand();
      AppUtils.log.info("initClubBrand done!");
  }
  
  /**
   * 初始化车友会品牌数据
   *
   * @param petrolMap
   * @throws SQLException
   */
  private void initClubBrand() {
      Connection conn = DBUtils.getConnection();
      boolean oldCommit = DBUtils.getAutoCommitValue(conn);
      PreparedStatement ps = null;
      ResultSet rs = null;
      try {
          conn.setAutoCommit(false);
          log.info("---->初始化车友会品牌数据---->准备执行……");
          long start = System.currentTimeMillis();
          /**
           * 获得ac_club表所有brand_id,brand_name
           */
          List<ClubBrand> listBrands = new ArrayList<ClubBrand>();
          ps = conn.prepareStatement(getAllClubBrandSql);
          rs = ps.executeQuery();
          while (rs.next()) {
        	  ClubBrand clubBrand = new ClubBrand();
        	  long brandId = rs.getLong("brand_id");
        	  String brandName = rs.getString("brand_name");
        	  if(brandId != 0 && brandName != null
        			  && !brandName.isEmpty()){
        		  clubBrand.setBrandId(brandId);
            	  clubBrand.setBrandName(brandName);
      	    	  String initial = Pinyin4jUtil.getPinyinToUpperCase(brandName);
      	    	  if(initial.length()>1){
     	    		 initial = initial.substring(0, 1);
     	    	  }
     	    	  initial = Pinyin4jUtil.specialHandling(brandName, initial);
     	    	  clubBrand.setInitial(initial);
     	    	  listBrands.add(clubBrand);
        	  }        	 	   
          }

          //更新车友会品牌
          for(ClubBrand brand : listBrands){
              ps = conn.prepareStatement(updateClubBrandSql);
              ps.setLong(1, brand.getBrandId());   
              ps.setString(2, brand.getBrandName());
              ps.setString(3, brand.getInitial());
              ps.executeUpdate();
          }

          long end = System.currentTimeMillis();
          log.info("耗时{}秒", (end - start) / 1000.0);
          log.info("---->初始化车友会品牌数据---->执行完毕！");
      } catch (Exception e) {
          log.error("initClubBrand exception!", e);
      } finally {
          try {
			conn.commit();
	        conn.setAutoCommit(oldCommit);
	        DBUtils.releaseConn(conn, ps, rs);
		} catch (SQLException e) {
			 log.error("initClubBrand db release throws Exception", e);
			e.printStackTrace();
		}


      }

  }
  
}
